// Employee's demographics
$(document).ready(function(){
$('select#province_id').change( function() {
		$.ajax ({
			type:     "GET",
			url:	  "../Patient/getDistrictFromProvinceAjax?set_ajax_view",
			data:	  "provice_id=" + $('select#province_id').val(),
			dataType: "json",
			success:  function(data) {
				$('select#district_id').html(data.districtList);
				$('select#ward_id').html(data.wardList);
			}
			});
	});
	
	$('select#district_id').change( function() {
		$.ajax ({
			type:    "GET",
			url:	 "../Patient/getWardFromDistrictAjax?set_ajax_view",
			data:	 "district_id=" + $('select#district_id').val(),
			success: function(data) {
				$('select#ward_id').html(data);
			}
			});
	});
	
	$('input#personName').blur(function() {
    	isNotEmpty($(this), _("Empty name"), 'statusMessage');
    });
    
    $('input#person_phone_number').blur(function() {
    	isNotEmpty($(this), _("Emty phone number"), 'statusMessage', '#AAA');
    });
     
    $('input#person_email').blur(function() {
    	checkEmail($(this), _("Invalid email"), 'statusMessage', '#AAA');
    });
   
    $('input#birthday').blur(function() {
    	checkBirthday($(this));
    });
    
    $('input#adrStreet').blur(function() {
    	isNotEmpty($(this), _("Empty address"), 'statusMessage');
    });
});

function checkBirthday(obj) {
	var res = isNotEmpty(obj, _('Empty date of birth'), 'statusMessage');
	if (res) {
   		var res = isDate(obj.val(), true);
   		if(res != true) {
   			obj.css("border-color", "red");
  			$('span#statusMessage').text(res).show();
  			return false;
   		} else {
   			$('input#person_age').css("border-color", "#AAA");
   			return true;
   		} 
   	}
   	return false;
}

function checkHiredDate(obj){
	var res = isNotEmpty(obj, _('Empty date'), 'statusMessageAppointment');
	if(res){
		var res = isDate(obj.val(), false);
		if(res != true) {
   			obj.css("border-color", "red");
  			$('span#statusMessageAppointment').text(res).show();
  			return false;
   		} else {
   			obj.css("border-color", "#AAA");
   			return true;
   		}
	}
	return false;
}

function checkExpiredDate(obj){
	if(obj.val().length==0){
		obj.css("border-color", "#AAA");
		$('span#statusMessageAppointment').text('');
		return true;
	}
	var res = isDate(obj.val(), false);
	if(res != true) {
		obj.css("border-color", "red");
		$('span#statusMessageAppointment').text(res).show();
		return false;
	} else {
		var cmp	=	compareDate(obj.val(),$('div#divEmployeeAppointmentEdit #fromDate').val());
		if(cmp=='lt'){
			obj.css("border-color", "red");
			$('span#statusMessageAppointment').text(_('End date is less than effective date')).show();
			return false;
		}
		$('span#statusMessageAppointment').text('');
		obj.css("border-color", "#AAA");
		return true;
	}
	
	return false;
}

function isReadyToSubmitDemographics() {
	if (!isNotEmpty($('input#personName'), _('Empty name'), 'statusMessage')) {
		return false;
	}
	if(!checkSelectGender()){
		return false;
	}
	if (!checkBirthday($('input#birthday'))) {
		return false;
	}
	if(!isNotEmpty($('input#person_phone_number'), _("Emty phone number"), 'statusMessage', '#AAA')){
		return false;
	}
	if (!checkNumber($('input#person_phone_number'), _("Invalid phone number"), 'statusMessage', '#AAA')) {
		return false;
	}
	if (!checkEmail($('input#person_email'), _("Invalid email"), 'statusMessage', '#AAA')) {
		return false;
	}
	return true;
}

function checkSelectGender() {
	if ($('#genderMale').is(':checked') || $('#genderFemale').is(':checked')) {
		$('span#statusMessage').text("").hide();
		return true;
	}
	$('span#statusMessage').text(_("Not yet select gender")).show();
  	return false;
}

function submitDemographics(){
	var res = isReadyToSubmitDemographics();
	if(!res){
		return;
	}
	$('div#employeeBarLoading').show('fast');
	$('div#divEmployeeDemographicsEdit').hide('fast');
	$('form#frmEmployeeDemographicsEdit').submit();
}

function showMessage(response,status){
	if(status=='success' && response.saved){
		$('div#employeeBarLoading').hide('fast');
		$('div#divEmployeeAppointmentEdit').show('fast');
		$('form#frmEmployeeAppointmentEdit #personId').val(response.person.id);
		$('div#divEmployeeAppointmentEdit #personName').text(response.person.name);
		$('form#frmEmployeeDemographicsEdit #personId').val(response.person.id);
		$('form#frmEmployeeUserAccount #personId').val(response.person.id);
		$('div#divEmployeeAccount #employeeName').text(response.person.name);
	}
	else{
		$('div#employeeBarLoading').hide('fast');
		$('div#divEmployeeDemographicsEdit').show('fast');
		$('span#statusMessage').text(_('Storage failed! Please try again.')).show();
	}
}
function gotoStep2(){
	$('div#divEmployeeDemographicsEdit').hide("fast");
	resetDemographicsButtonStates();
	$('div#divEmployeeAppointmentEdit').show("fast");
}

function resetDemographicsButtonStates(){
	$('div#divDemegraphicsAddBtn').css('display','block');
	$('div#divGotoStep2Btn').css('display','none');
}
$(document).ready(function(){
	 var options = { 
        success: showMessage,
        url:  $('#frmEmployeeDemographicsEdit').attr('action')+'?set_ajax_view',
        dataType:  'json'      
    }; 
    $('#frmEmployeeDemographicsEdit').ajaxForm(options); 
})


// Appointments of employee
function backtoStep1(){
	$('div#divEmployeeAppointmentEdit').hide("fast");
	$('div#divEmployeeDemographicsEdit').show("fast");
	$('div#divGotoStep2Btn').css("display","block");
}

function gotoStep3(){
	$('div#divEmployeeAppointmentEdit').hide("fast");
	resetAppointmentButtonStates();
	$('div#divEmployeeAccount').show("fast");
}

function resetAppointmentButtonStates(){
	$('div#divGotoStep3').css('display','none');
}

function isReadyToSubmitAppointment(){
   	if(!checkHiredDate($('input#fromDate'))){
    	return false;
    }
    
    if(!checkExpiredDate($('input#toDate'))){
    	return false;
    };
    
    return true;
}
function submitAppointments(){
	if(!isReadyToSubmitAppointment()){
		return;
	}
	$('div#employeeBarLoading').show('fast');
	$('div#divEmployeeAppointmentEdit').hide('fast');
	$('#frmEmployeeAppointmentEdit').submit();
}

function saveAndToStep3(){
	if(!isReadyToSubmitAppointment()){
		return;
	}
	var submitData	=	$('#frmEmployeeAppointmentEdit').formSerialize();
	$('div#employeeBarLoading').show('fast');
	$('div#divEmployeeAppointmentEdit').hide('fast');
	$.ajax ({
		type:   "post",
		url:	  $('#frmEmployeeAppointmentEdit').attr('action')+'?set_ajax_view',
		data:	  submitData,
		success:	function(data) {
			$('div#employeeBarLoading').hide('fast');
			$('#divEmployeeAccount').show('fast');
		}
	});
}

function showAppointmentEditMessage(response,status){
	if(status=='success' && response.saved){
		$('div#divAppointmentEditMessage #lblEditMessage').text(_('Creating new employee's data succeed'));
	}
	else{
		$('div#divAppointmentEditMessage #lblEditMessage').text(_('Fail! Please try again'));
	}
	$('div#employeeBarLoading').hide('fast');
	$('div#divAppointmentEditMessage').show('fast');
}

$(document).ready(function(){
	 var options = { 
        success: showAppointmentEditMessage,
        url:  $('#frmEmployeeAppointmentEdit').attr('action')+'?set_ajax_view',
        dataType:  'json'      
    }; 
    $('#frmEmployeeAppointmentEdit').ajaxForm(options); 
    
    $('input#fromDate').blur(function() {
    	checkHiredDate($(this));
    });
    
    $('input#toDate').blur(function() {
    	checkExpiredDate($(this));
    });
})

// User account for employee
$(document).ready(function(){
	var options = { 
        success: showUserAccountEditMessage,
        url:  $('#frmEmployeeUserAccount').attr('action')+'?set_ajax_view',
        dataType:  'json'      
    }; 
    $('#frmEmployeeUserAccount').ajaxForm(options); 
    
    $('input#employeeAccountName').blur(function(){
    	isNotEmpty($(this), _("Empty name"), 'statusMessageEmployeeAccount');
    });
    
    $('input#employeeAccountPassword').blur(function(){
    	isNotEmpty($(this), _("Empty password"), 'statusMessageEmployeeAccount');
    });
    
    $('input#employeePasswordMatch').blur(function(){
    	checkPasswordMatch($(this), $('#employeeAccountPassword'), _("Password doesn't match"), 'statusMessageEmployeeAccount');
    });
    resetUserNamePassword();
});

function resetUserNamePassword(){
	$('input#employeeAccountName').val('');
	$('input#employeeAccountPassword').val('');
	$('input#employeePasswordMatch').val('');
}
function checkPasswordMatch(obj, pwdObj, message, target){
	if(obj.val() == pwdObj.val()){
		$("span#"+target).text('');
		obj.css("border-color", "#AAA");
		return true;
	}
	else{
		$("span#"+target).text(message).show();
		obj.css("border-color","red");
		return false;
	}
}

function submitAccount(){
	
	if(!isReadyToSubmitAccount()){
		return;
	}
	$('div#employeeBarLoading').show('fast');
	$('div#divEmployeeAccount').hide('fast');
	$('#frmEmployeeUserAccount').submit();
}

function isReadyToSubmitAccount(){
	if(!isNotEmpty($('input#employeeAccountName'), _("Empty name"), 'statusMessageEmployeeAccount')){
		return false;
	}
	
	if(!isNotEmpty($('input#employeeAccountPassword'), _("Empty password"), 'statusMessageEmployeeAccount')){
		return false;
	}
	
	if(!checkPasswordMatch($('input#employeePasswordMatch'), $('#employeeAccountPassword'), _("Password doesn't match"), 'statusMessageEmployeeAccount')){
		return false;
	}
	
	if(!checkSelectDisabled()){
		return false;
	}	
	
	if(!checkSelectRole()){
		return false;
	}
	
	return true;
}

function showUserAccountEditMessage(response,status){
	if(status=='success' && response.saved){
		$('div#divUserAccountEditMessage #lblEditMessage').text(_('Creating new employee's data succeed'));
	}
	else if(response.error=='username duplicated'){
			$('div#divEmployeeAccount #statusMessageEmployeeAccount').text(_('Username has been used! Please choose another')).show();
			$('div#employeeBarLoading').hide('fast');
			$('div#divEmployeeAccount').show('fast');
			return;
		}
		else{
			$('div#divUserAccountEditMessage #lblEditMessage').text(_('Fail! Please try again'));
		}
	$('div#employeeBarLoading').hide('fast');
	$('div#divUserAccountEditMessage').show('fast');
}

function checkSelectDisabled() {
	if ($('#disabledYes').is(':checked') || $('#disabledNo').is(':checked')) {
		$('span#statusMessageEmployeeAccount').text("").hide();
		return true;
	}
	$('span#statusMessageEmployeeAccount').text(_("Not yet select the attribute \"Lock\"")).show();
  	return false;
}

function checkSelectRole(){
	if($('#groups').val()==null || $('#groups').val().length==0){
		$('span#statusMessageEmployeeAccount').text(_("Not yet select role in the system")).show();
		return false;
	}
	$('span#statusMessageEmployeeAccount').text('').hide();
	return true;
}

function backtoStep2(){
	$('div#divEmployeeAccount').hide("fast");
	$('div#divGotoStep3').css('display','block');
	$('div#divEmployeeAppointmentEdit').show("fast");
}
